﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Xml;
using System.Management;
using System.Threading;
using System.Threading.Tasks;


namespace WMIInventoryClient
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        public bool CheckObligatoryFields()
        {
            if (textBoxName.TextLength != 0 || textBoxSubdivision.TextLength != 0)
            {
                if (textBoxName.TextLength != 0) textBoxName.BackColor = SystemColors.Window;
                else 
                {
                    textBoxName.BackColor = Color.LightPink;
                    return false;
                }
                if (textBoxSubdivision.TextLength != 0) textBoxSubdivision.BackColor = SystemColors.Window;
                else
                {
                    textBoxSubdivision.BackColor = Color.LightPink;
                    return false;
                }

                return true;
            }
            else
            {
                if(textBoxName.TextLength == 0) textBoxName.BackColor = Color.LightPink;
                if (textBoxSubdivision.TextLength == 0) textBoxSubdivision.BackColor = Color.LightPink;
                return false;
            }
        }

        private void buttonReport_Click(object sender, EventArgs e)
        {
            if (CheckObligatoryFields())
            {
                OnOffEnabled(this,this,false);
                ManagementObjectSearcher s = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
                XmlDocument doc = new XmlDocument();
                doc.LoadXml("<WMIInventory></WMIInventory>");
                XmlNode xmlNode;

                xmlNode = doc.CreateElement("DateTime");
                xmlNode.InnerText = DateTime.Now.ToString();
                doc.DocumentElement.AppendChild(xmlNode);

                foreach (ManagementObject service in s.Get())
                {
                    listBoxLog.Items.Add("Registered user: " + service["RegisteredUser"]);
                    xmlNode = doc.CreateElement("RegisteredUser");
                    xmlNode.InnerText = service["RegisteredUser"].ToString();
                    doc.DocumentElement.AppendChild(xmlNode);
                }

                s.Query.QueryString = "SELECT * FROM Win32_ComputerSystem";
                foreach (ManagementObject service in s.Get())
                {
                    listBoxLog.Items.Add("Computer name: " + service["Name"]);
                    xmlNode = doc.CreateElement("ComputerName");
                    xmlNode.InnerText = service["Name"].ToString();
                    doc.DocumentElement.AppendChild(xmlNode);
                }

                s.Query.QueryString = "SELECT * FROM Win32_NetworkAdapter";
                foreach (ManagementObject service in s.Get())
                {
                    try
                    {
                        if (service["MACAddress"].ToString().Trim().Length > 0)
                        {
                            listBoxLog.Items.Add("MAC-address: " + service["MACAddress"]);
                            xmlNode = doc.CreateElement("MACAddress");
                            xmlNode.InnerText = service["MACAddress"].ToString();
                            doc.DocumentElement.AppendChild(xmlNode);
                        }
                    }
                    catch (Exception eX)
                    {

                    }
                }

                listBoxLog.Items.Add("Windows product key: " + KeyDecoder.DecodeProductKey(KeyDecoder.GetRegistryDigitalProductId(KeyDecoder.Key.Win)).ToString());
                xmlNode = doc.CreateElement("WinProductKey");
                xmlNode.InnerText = KeyDecoder.DecodeProductKey(KeyDecoder.GetRegistryDigitalProductId(KeyDecoder.Key.Win)).ToString();
                doc.DocumentElement.AppendChild(xmlNode);

                s.Query.QueryString = "SELECT * FROM Win32_Product";
                foreach (ManagementObject service in s.Get())
                {
                    listBoxLog.Items.Add("Install program: " + service["Name"]);
                    xmlNode = doc.CreateElement("ProgramName");
                    xmlNode.InnerText = service["Name"].ToString();
                    doc.DocumentElement.AppendChild(xmlNode);
                }
                                
                string FileName = string.Empty;

                XmlNodeList NodesByName = doc.GetElementsByTagName("ComputerName");
                foreach (XmlNode node in NodesByName)
                    if (node.InnerText.ToString().Length > 0)
                    {
                        FileName = node.InnerText.ToString();
                        break;
                    }
                NodesByName = doc.GetElementsByTagName("MACAddress");
                foreach (XmlNode node in NodesByName)
                    if (node.InnerText.ToString().Length > 0)
                    {
                        FileName += ". " + node.InnerText.Replace(":", "-").ToString();
                        break;
                    }
                doc.Save(linkLabelPath.Text + "\\" + FileName + ".xml");
                OnOffEnabled(this, this,true);
            }
        }

        public static void OnOffEnabled(FormMain tmpForm, Control control, bool flag)
        {
            if (!flag) control.Enabled = false; else control.Enabled = true;

            foreach (Control c in control.Controls)
                OnOffEnabled(tmpForm, c, flag);
        }


        private void textBoxName_KeyDown(object sender, KeyEventArgs e)
        {
            CheckObligatoryFields();
        }

        private void textBoxSubdivision_KeyDown(object sender, KeyEventArgs e)
        {
            CheckObligatoryFields();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            linkLabelPath.Text = Application.StartupPath;
        }
        
        private void linkLabelPath_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (linkLabelPath.Text.Length != 0) Process.Start(linkLabelPath.Text);
        }
    }
}
